PHPのType Declaration
v5.xまではPHPのType Hintingと呼ばれていたもの
PHPのv7.4から入った型検査するやつ
型は明示しているだけで、異なった値を与えてもエラーにはならない
PHP Intelephenseはエラーを出してくれる
strict_typesを使用しない場合は暗黙のcastが行われる
RFC
docs
型の種類
bool, int, float, string, array, object
iterable
self, parent
任意のクラスおよびインターフェイス名
?type
nullable
e.g. ?int, ?string
declare(strict_types=1)
Undefined typeというエラーはなに?
SymfonyをDockerで動かしてるからかなmrsekut.icon
vendorディレクトリをsyncしてなかったからだった
PHP Intelephenseに依るもの
Stubを追加するなどもあるらしい
https://github.com/bmewburn/vscode-intelephense/issues/1291
PHPの型
https://qiita.com/rana_kualu/items/9b4502d229d19635ef34
PHPStan
#wip
型宣言は実行時に影響がある
https://zenn.dev/tadsan/scraps/b943c7f29f6dea
GPT-4.icon
union types
v8.0以降
code:php
function processInput(int|string $input): void {}
null許容型
code:php
function getUserName(?int $userId): ?string {}
配列型
array
v8.1からiterableを使える
genricsの表現ができる?
callable型
code:php
function executeCallback(callable $callback): void {
$callback();
}
declare(strict_types=1)